<?php
class TaskReturnModel extends BaseModel
{
	/**
	 * 真对任务执行者返回需求退回任务，并且已经被处理过
	 * 返回当前用户因为需求有问题退回的任务
	 * 已经经过处理任务已经解决
	 * 必须满足任务：1等待处理 和 退回处理状态为 2已经解决
	 * @param int $tid
	 * @return int 数量
	 * @author ice<iceinto@mallog.com.cn>
	 */
	public function getHaveReturnCount($uid){
		$map = array();
		$map['m_task_return.status'] = 2;
		$map['m_task.status'] = 1;
		$map['m_task_allot.uid'] = $uid;
		return $this->join('INNER JOIN m_task ON m_task_return.tid = m_task.id')
		->join('INNER JOIN m_task_allot ON m_task_return.tid = m_task_allot.tid')
		->where($map)
		->count();
	}

	/**
	 * Returns a be modified demand return mission
	 * @param int $uid user id
	 * @author ice<iceinto@mallog.com.cn>
	 */
	public function getReturnOne($uid){
		$map = array();
		$map['m_task_return.status'] = 2;
		$map['m_task.status'] = 1;
		$map['m_task_allot.uid'] = $uid;
		$task = $this->field('m_task.id,m_task.title')
		->join('INNER JOIN m_task ON m_task_return.tid = m_task.id')
		->join('INNER JOIN m_task_allot ON m_task_return.tid = m_task_allot.tid')
		->where($map)
		->find();
		return $task;
	}

	/**
	 * 获取返回
	 * @param int $tid task
	 * @author ice<iceinto@mallog.com.cn>
	 */
	public function getReturn($tid){
		$map = array();
		$map['tid'] = $tid;
		$map['status'] = 1;
		$returns = $this->where($map)
		->field('id,create_time,uid,remark,reply,reply_time')
		->order('create_time DESC')
		->select();
		foreach($returns as $key=>$return){
			$user = D('User')->getById($return['uid'], 'num');
			$returns[$key]['num'] = $user['num'];
		}
		return $returns;
	}

	/**
	 * 获取所以返回
	 * @param int $tid task
	 * @author ice<iceinto@mallog.com.cn>
	 */
	public function getAllReturn($tid){
		$map = array();
		$map['tid'] = $tid;
		$returns = $this->where($map)
		->field('id,create_time,uid,remark,reply,reply_time')
		->order('create_time DESC')
		->select();
		foreach($returns as $key=>$return){
			$user = D('User')->getById($return['uid'], 'num');
			$returns[$key]['num'] = $user['num'];
		}
		return $returns;
	}

	/**
	 * 获取用户返回信息
	 * @param int $uid 用户标识
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getUserReturn($uid){
		$TaskModel = D('Task');
		$map = array();
		$tasks = array();
		$map['uid'] = $uid;
		$map['status'] = 1;
		$returnTasks = $this->where($map)->select();
		foreach($returnTasks as $task){
			$tasks[] = $TaskModel->field('id,title')->find($task['tid']);
		}
		return $tasks;
	}

	public function getAllotOneReturn($uid){
		$map = array();
		$map['m_task_return.status'] = 2;
		$map['m_task.status'] = 1;
		$map['m_task_allot.uid'] = $uid;
		$task = $this->field('m_task.id as id,m_task.title as title')
		->join('INNER JOIN m_task ON m_task_return.tid = m_task.id')
		->join('INNER JOIN m_task_allot ON m_task_return.tid = m_task_allot.tid')
		->where($map)
		->find();
		return $task;
	}
}